package com.cloud.bc.util;

import cn.hutool.core.bean.BeanUtil;
//import cn.hutool.core.img.GraphicsUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.img.ImgUtil;
import cn.hutool.core.io.BufferUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.extra.qrcode.QrCodeException;
import cn.hutool.extra.qrcode.QrCodeUtil;
import cn.hutool.extra.qrcode.QrConfig;
import com.cloud.bc.config.AliOssConfig;
import com.cloud.common.oss.AliOssRequest;
import com.cloud.common.oss.AliOssResponse;
import com.cloud.common.oss.AliUploadHelper;
import com.cloud.common.util.GraphicsUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Service;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;

@Service
public class QRCodeUtil {
    @Autowired
    AliOssConfig aliOssConfig;

    // 自定义参数，这部分是Hutool工具封装的
    private static QrConfig initQrConfig() {
        QrConfig config = new QrConfig(300, 300);
        // 设置边距，既二维码和背景之间的边距
        config.setMargin(3);
        // 设置前景色，既二维码颜色（青色）
        config.setForeColor(Color.CYAN.getRGB());
        // 设置背景色（灰色）
        config.setBackColor(Color.white.getRGB());
//        String path = Thread.currentThread().getContextClassLoader().getResource("").getPath()+"static/m/img/template/recruit.png";
        return config;
    }

    /**
     * 生成到文件
     *
     * @param content
     * @param filepath
     */
    public void createQRCode2File(String content, String filepath) {
        try {
            QrCodeUtil.generate(content, initQrConfig(), FileUtil.file(filepath));
//            log.info("生成二维码成功, 位置在：{}！", filepath);
        } catch (QrCodeException e) {
//            log.error("发生错误！ {}！", e.getMessage());
        }
    }

    /**
     * 生成到流
     *
     * @param content
     * @param response
     */
    public void createQRCode2Stream(String content, HttpServletResponse response) {
        try {
            QrCodeUtil.generate(content, initQrConfig(), "png", response.getOutputStream());
//            log.info("生成二维码成功!");
        } catch (QrCodeException | IOException e) {
//            log.error("发生错误！ {}！", e.getMessage());
        }
    }

    /**
     * 上传到OSS
     *
     * @param content
     * @param response
     */
    public String createQRCode2Oss(String content, HttpServletResponse response, String writeFilePath) {
        try {
            BufferedImage bufferedImage =
                    QrCodeUtil.generate(content, initQrConfig());
//            String targetFilePath = Thread.currentThread().getContextClassLoader().getResource("").getPath() + "static/m/img/template/recruit.png";
//            File blankFile = new File(targetFilePath);
            ClassPathResource resource = new  ClassPathResource("static/m/img/template/recruit.png");
            InputStream inputStream = resource.getInputStream();
            InputStream recruitQrCodeInputStream = GraphicsUtil.merge(ImageIO.read(inputStream), bufferedImage, 179, 630, 235, 235);
//            saveBit(recruitQrCodeInputStream);
            AliOssRequest aliOssRequest = new AliOssRequest();
            BeanUtil.copyProperties(aliOssConfig, aliOssRequest);
            AliUploadHelper aliUploadHelper = new AliUploadHelper(aliOssRequest);
            AliOssResponse aliOssResponse =
                    aliUploadHelper.uploadSimple(recruitQrCodeInputStream, writeFilePath);
            return aliOssResponse.getAbsolutePath();
        } catch (QrCodeException | IOException e) {
            System.out.println(e.getMessage());
        }
        return "";
    }
}
